import { Controller, Get, Inject } from '@midwayjs/core';
import { UserService } from '../service/user.service';

@Controller('/api/mock')
export class MockController {
  @Inject()
  userService: UserService;

  @Get('/login')
  async mockLogin() {
    // 模拟一个固定的微信用户信息
    const mockWechatUser = {
      openid: 'o_test_openid_123456789',
      nickname: '模拟用户',
      sex: 1,
      language: 'zh_CN',
      city: '广州',
      province: '广东',
      country: '中国',
      headimgurl:
        'https://thirdwx.qlogo.cn/mmopen/g3MonUZtNHkdmzicIlibx6iaFqAc56vxLSUfpb6n5WKSYVY0ChQKkiaJSgQ1dZuTOgvLLrhJbERQQ4eMsv84eavHiaiceqxibJxCfHe/0',
      privilege: [],
      unionid: 'o_test_unionid_123456789',
    };

    // 查找或创建这个模拟用户
    const user = await this.userService.findOrCreateUserByWechat(
      mockWechatUser
    );

    // 生成一个模拟 token
    const token = 'mock_token_for_' + user.id;

    // 返回前端需要的信息
    return {
      success: true,
      data: {
        token,
        userInfo: user,
      },
    };
  }
}
